package com.example.controller;

import com.example.pojo.Person;
import com.example.service.PersonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author およそ神
 * @version JDK 1.8
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonService personService;
    /**
     * 根据id查询部门信息
     * 如果根据id查询出现异常,则走hystrixGet这段备选代码
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/searchById/{id}")
    public Person searchById(@PathVariable("id") Integer id) {
        System.out.println("进入PersonController的searchById方法！" + id);
         Person person = this.personService.searchById(id);
        if (person == null) {
            throw new RuntimeException("进入熔断方法！");
        }
        return person;
    }

    /**
     * 根据id查询备选方案(熔断)
     * @param id
     * @return
     */
    public Person hystrixGet(@PathVariable("id") Integer id){
        System.out.println("进入熔断方法hystrixGet！" + id);
        return new Person().setId(id)
                .setName("这个id=>"+id+",没有对应的信息,null---@Hystrix~")
                .setAge(666)
                .setUpdateby("凡总神")
                .setTest("test---->成功！");
    }

}
